package com.sctvcloud.qingchuan.ui.adapter.holder;

import android.content.Context;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Pair;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.beans.PoliticsReplyDotBean;
import com.sctvcloud.qingchuan.ui.adapter.PoliticsDetailReplyListAdapter;
import com.sctvcloud.qingchuan.ui.utils.GlideCircleTransform;
import com.sctvcloud.qingchuan.ui.utils.GlideUtil;

import java.util.ArrayList;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 问政详情的回复列表
 */
public class PoliticsReplyHolder extends BaseAbsHolder<PoliticsReplyDotBean> implements View.OnClickListener {

    @BindView(R.id.author_reply_content)
    protected CustomFontTextView author_reply_content;
    @BindView(R.id.reply_time)
    protected CustomFontTextView reply_time;
    @BindView(R.id.author_name)
    protected CustomFontTextView author_name;
    @BindView(R.id.author_avatar)
    protected ImageView author_avatar;


    @BindView(R.id.replay_layout)
    protected LinearLayout replay_layout;
    @BindView(R.id.user_reply_list)
    protected RecyclerView user_reply_list;
    @BindView(R.id.reply_send)
    protected CustomFontTextView reply_send;
    @BindView(R.id.reply_more)
    protected CustomFontTextView reply_more;

    @BindView(R.id.top_line)
    protected View top_line;


    private OnLoadMoreReplyClickListener loadMoreReplyClickListener;
    private PoliticsUserReplyHolder.OnReplyClickListener replyClickListener;
    private PoliticsDetailReplyListAdapter questioningAdapter;
    private PoliticsReplyDotBean temp;


    public PoliticsReplyHolder(Context context, View itemView, OnLoadMoreReplyClickListener loadMoreReplyClickListener, PoliticsUserReplyHolder.OnReplyClickListener replyClickListener) {
        super(context, itemView);
        itemView.setOnClickListener(this);
        this.loadMoreReplyClickListener = loadMoreReplyClickListener;
        this.replyClickListener = replyClickListener;
    }


    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(PoliticsReplyDotBean t) {
        temp = t;
        author_reply_content.setText(t.getContent());
        reply_time.setText(DateUtils.formatDate(DateUtils.getDataDate(t), ""));
        author_name.setText(t.getNickName());
        GlideUtil.getGlideWithAvatorDef(context, t.getAvatar()).transform(new GlideCircleTransform(context)).into(author_avatar);

        user_reply_list.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        user_reply_list.setItemAnimator(new DefaultItemAnimator());
        questioningAdapter = new PoliticsDetailReplyListAdapter(context, temp.getInteractionReplyDtoList(), replyClickListener);
        user_reply_list.setAdapter(questioningAdapter);
        if (ListUtils.isListValued(temp.getInteractionReplyDtoList())) {
            reply_more.setVisibility(View.VISIBLE);
            replay_layout.setVisibility(View.VISIBLE);
        } else {
            reply_more.setVisibility(View.GONE);
//            replay_layout.setVisibility(View.GONE);
        }
        reply_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != loadMoreReplyClickListener) {
                    loadMoreReplyClickListener.OnLoadMoreReply(temp);
                }
            }
        });
        reply_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 追问
                if (null != replyClickListener) {
                    replyClickListener.OnReplyClick(temp);
                }
            }
        });
        if (t.getPosition() > 0) {
            top_line.setVisibility(View.VISIBLE);
        } else {
            top_line.setVisibility(View.GONE);
        }
    }

    @Override
    public void setItemMode(int mode) {
        super.setItemMode(mode);
//        if (mode == ITEMMODE_LAST) {
//            bottomLine.setVisibility(View.GONE);
//        } else {
////            bottomLine.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (temp == null) {
            Toast.makeText(context, "数据错误", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
        pairs.add(new Pair<>("页面", "首页"));
        pairs.add(new Pair<>("栏目", "精选"));
        pairs.add(new Pair<>("位置", "24小时"));
        ArrayList<Pair<String, String>> tempPair = new ArrayList<>();
        tempPair.addAll(pairs);
        tempPair.add(new Pair<>("行为类型", "点击"));

        if (activity != null) {
            GridsumWebDissector.getInstance().trackEvent(activity, "", temp.getTitle(), "", 200, tempPair);
        } else if (fragment != null) {
            GridsumWebDissector.getInstance().trackEvent(fragment, "", temp.getTitle(), "", 200, tempPair);
        }
        HashMap<String, String> map = new HashMap<>();
        for (Pair<String, String> p : pairs) {
            map.put(p.first, p.second);
        }


    }

    public interface OnLoadMoreReplyClickListener {
        void OnLoadMoreReply(PoliticsReplyDotBean t);
    }
}
